home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / php / PEAR / Net / DNSBL.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  6.4 KB  |  233 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * PEAR::Net_DNSBL
  7.  *
  8.  * This class acts as interface to generic Realtime Blocking Lists
  9.  * (RBL)
  10.  *
  11.  * PHP versions 4 and 5
  12.  *
  13.  * LICENSE: This source file is subject to version 3.01 of the PHP license
  14.  * that is available through the world-wide-web at the following URI:
  15.  * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
  16.  * the PHP License and are unable to obtain it through the web, please
  17.  * send a note to license@php.net so we can mail you a copy immediately.
  18.  *
  19.  * Net_DNSBL looks up an supplied host if it's listed in 1-n supplied
  20.  * Blacklists
  21.  *
  22.  * @category   Net
  23.  * @package    DNSBL
  24.  * @author     Sebastian Nohn <sebastian@nohn.net>
  25.  * @author     Ammar Ibrahim <fixxme@fixme.com>
  26.  * @copyright  2004-2007 Sebastian Nohn <sebastian@nohn.net>
  27.  * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  28.  * @version    CVS: $Id: DNSBL.php,v 1.4 2006/12/25 10:40:59 nohn Exp $
  29.  * @link       http://pear.php.net/package/Net_DNSBL
  30.  * @see        Net_DNS
  31.  * @since      File available since Release 1.0.0
  32.  */
  33.  
  34. require_once 'Net/CheckIP.php';
  35. require_once 'Net/DNS.php';
  36.  
  37. class Net_DNSBL {
  38.  
  39.     /**     
  40.      * Array of blacklists.
  41.      *
  42.      * Must have one or more elements.
  43.      *
  44.      * @var    array
  45.      * @access protected
  46.      */
  47.     var $blacklists = array('sbl-xbl.spamhaus.org',
  48.                             'bl.spamcop.net');
  49.  
  50.     /**     
  51.      * Array of Results
  52.      *
  53.      * @var    array
  54.      * @access protected
  55.      */
  56.     var $results    = array();
  57.  
  58.     /**
  59.      * Set the blacklist to a desired blacklist.
  60.      *
  61.      * @param  array Array of blacklists to use. May contain only one element.
  62.      * @access public
  63.      * @return bool true if the operation was successful
  64.      */
  65.     function setBlacklists($blacklists)
  66.     {
  67.         if (is_array($blacklists)) {
  68.             $this->blacklists = $blacklists;
  69.             return true;
  70.         } else {
  71.             return false;
  72.         } // if
  73.     } // function
  74.  
  75.     /**
  76.      * Get the blacklists.
  77.      *
  78.      * @access public
  79.      * @return array Currently set blacklists.
  80.      */
  81.     function getBlacklists()
  82.     {
  83.         return $this->blacklists;
  84.     }
  85.  
  86.     /** 
  87.      * Returns Blacklist and Reply from the Blacklist, a host is listed in.
  88.      *
  89.      * @param  string Host to check
  90.      * @access public
  91.      * @return array result. $result['dnsbl'] contains DNSBL,
  92.      *               $result['record'] contains returned DNS record.
  93.      */
  94.     function getDetails($host)
  95.     {
  96.         if (isset($this->results[$host]['dnsbl'])) {
  97.             return $this->results[$host];
  98.         } else {
  99.             return false;
  100.         }
  101.     } // function
  102.  
  103.     /**
  104.      * Returns Blacklist, host is listed in.
  105.      *
  106.      * @param  string Host to check
  107.      * @access public
  108.      * @return bl, a host is listed in or false
  109.      */
  110.     function getListingBl($host)
  111.     {
  112.         if (isset($this->results[$host]['dnsbl'])) {
  113.             return $this->results[$host]['dnsbl'];
  114.         } else {
  115.             return false;
  116.         }
  117.     } // function
  118.  
  119.     /**
  120.      * Returns result, when a host is listed.
  121.      *
  122.      * @param  string Host to check
  123.      * @access public
  124.      * @return bl, a host is listed in or false
  125.      */
  126.     function getListingRecord($host)
  127.     {
  128.         if (isset($this->results[$host]['record'])) {
  129.             return $this->results[$host]['record'];
  130.         } else {
  131.             return false;
  132.         }
  133.     } // function
  134.  
  135.     /**
  136.      * Returns TXT-Records, when a host is listed.
  137.      *
  138.      * @param  string Host to check
  139.      * @access public
  140.      * @return array TXT-Records for this host
  141.      */
  142.     function getTxt($host)
  143.     {
  144.         if (isset($this->results[$host]['txt'])) {
  145.             return $this->results[$host]['txt'];
  146.         } else {
  147.             return false;
  148.         }
  149.     } // function
  150.  
  151.     /** 
  152.      * Checks if the supplied Host is listed in one or more of the
  153.      * RBLs.
  154.      *
  155.      * @param  string Host to check for being listed.
  156.      * @access public
  157.      * @return boolean true if the checked host is listed in a blacklist.
  158.      */
  159.     function isListed($host)
  160.     {
  161.         $isListed = false;
  162.         $resolver = new Net_DNS_Resolver;
  163.  
  164.         foreach ($this->blacklists as $blacklist) {
  165.             $response = $resolver->query($this->getHostForLookup($host, $blacklist));
  166.             if ($response) {
  167.                 $isListed = true;
  168.                 $this->results[$host]['dnsbl']  = $blacklist;
  169.                 $this->results[$host]['record'] = $response->answer[0]->address;
  170.                 $response_txt = $resolver->query($this->getHostForLookup($host, $blacklist), 'TXT');
  171.                 foreach ($response_txt->answer as $txt) {
  172.                     $this->results[$host]['txt'][] = $txt->text[0];
  173.                 }
  174.                 //if the Host was listed we don't need to check other RBLs,
  175.                 break;
  176.                 
  177.             } // if
  178.         } // foreach
  179.         
  180.         return $isListed;
  181.     } // function
  182.  
  183.     /** 
  184.      * Get host to lookup. Lookup a host if neccessary and get the
  185.      * complete FQDN to lookup.
  186.      *
  187.      * @param  string Host OR IP to use for building the lookup.
  188.      * @param  string Blacklist to use for building the lookup.
  189.      * @access protected
  190.      * @return string Ready to use host to lookup
  191.      */    
  192.     function getHostForLookup($host, $blacklist) 
  193.     {
  194.         // Currently only works for v4 addresses.
  195.         if (!Net_CheckIP::check_ip($host)) {
  196.             $resolver = new Net_DNS_Resolver;
  197.             $response = $resolver->query($host);
  198.             $ip = $response->answer[0]->address;
  199.         } else {
  200.             $ip = $host;
  201.         }
  202.  
  203.         return $this->buildLookUpHost($ip, $blacklist);
  204.     } // function
  205.  
  206.     /**
  207.      * Build the host to lookup from an IP.
  208.      *
  209.      * @param  string IP to use for building the lookup.
  210.      * @param  string Blacklist to use for building the lookup.
  211.      * @access protected
  212.      * @return string Ready to use host to lookup
  213.      */    
  214.     function buildLookUpHost($ip, $blacklist)
  215.     {
  216.         return $this->reverseIp($ip).'.'.$blacklist;        
  217.     } // function
  218.  
  219.     /**
  220.      * Reverse the order of an IP. 127.0.0.1 -> 1.0.0.127. Currently
  221.      * only works for v4-adresses
  222.      *
  223.      * @param  string IP to reverse.
  224.      * @access protected
  225.      * @return string Reversed IP
  226.      */    
  227.     function reverseIp($ip) 
  228.     {        
  229.         return implode('.', array_reverse(explode('.', $ip)));        
  230.     } // function
  231.  
  232. } // class
  233. ?>